package com.xtoneict.auth.service.impl;

import com.xtoneict.auth.entity.LeaveDetail;
import com.xtoneict.auth.entity.LeaveDetailHelper;
import com.xtoneict.auth.mapper.LeaveDetailMapper;
import com.xtoneict.auth.service.ILeaveDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hx
 * @since 2024-06-19
 */
@Service
public class LeaveDetailServiceImpl extends ServiceImpl<LeaveDetailMapper, LeaveDetail> implements ILeaveDetailService {


    /**
     * 正确格式："年假12-04 09:00到12-04 13:00 0.5天"
     *
     * @param records
     * @return
     */

    public List<com.xtoneict.auth.entity.LeaveDetailHelper> parseLeaveRecords(List<String> records) {
        List<LeaveDetailHelper> leaveDetails = new ArrayList<>();
        for (String record : records) {

            if(record.contains("迟到") || record.contains("早退")){
                continue;
            }

            Boolean status = false;

            String[] parts = record.split(" ");

            if (parts.length != 4 || !record.contains("到")){
                status = true;
                LeaveDetailHelper leaveDetail = new LeaveDetailHelper();
                leaveDetail.setStatus(status);
                leaveDetails.add(leaveDetail);
                continue;
            }

            //type
            String regex = "(\\D+)(\\d+-\\d+)";
            java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(regex);
            java.util.regex.Matcher matcher = pattern.matcher(parts[0]);
            String type = null;
            String startDate = null;
            if (matcher.find()) {
                type = matcher.group(1);
                startDate = matcher.group(2);
            }else {
                status = true;
                LeaveDetailHelper leaveDetail = new LeaveDetailHelper();
                leaveDetail.setStatus(status);
                leaveDetails.add(leaveDetail);
                continue;
            }

            //start date and end date
            String[] dateRange = parts[1].split("到");
            String startTime = dateRange[0];
            String endDate = dateRange[1];

            String endTime = parts[2];

            //days
            double days = Double.parseDouble(parts[3].replace("天", ""));

            LeaveDetailHelper leaveDetail = new LeaveDetailHelper();

                leaveDetail.setLeaveType(type);
                leaveDetail.setLeaveDays(days);
                leaveDetail.setStartDate(startDate);
                leaveDetail.setEndDate(endDate);
                leaveDetail.setStartTime(startTime);
                leaveDetail.setEndTime(endTime);
                leaveDetail.setStatus(status);

                leaveDetails.add(leaveDetail);
        }

        return leaveDetails;
    }
}
